package puffer.starter.db.entity;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 分页工具
 *
 * @author 王永吉
 */
public class Page<T> {
    long total = 0;
    long size = 10;
    long current = 1;
    long pages = 1;
    //是否为首页
    boolean isFirst;
    //是否为最后一页
    boolean isLast;
    List<T> data = Collections.emptyList();

    /**
     * current 页数（默认1）
     * pageSize 分页大小（默认10）
     */
    public Page(Map param) {
        if (param == null) {
            setCurrent(1L);
            setSize(10L);
            return;
        }
        Object current = param.get("current");
        Object pageSize = param.get("size");
        if (isBlank(current) || Integer.parseInt(current.toString()) <= 0) current = 1L;
        if (isBlank(pageSize) || Integer.parseInt(current.toString()) <= 0) pageSize = 10L;

        setCurrent(Long.parseLong(current + ""));
        setSize(Long.parseLong(pageSize + ""));
    }

    /**
     * current 页数
     * pageSize 分页大小
     */
    public Page(Object current, Object pageSize) {
        if (isBlank(current) || Integer.parseInt(current.toString()) <= 0) current = 1L;
        if (isBlank(pageSize) || Integer.parseInt(current.toString()) <= 0) pageSize = 10L;

        setCurrent(Long.parseLong(current + ""));
        setSize(Long.parseLong(pageSize + ""));
    }

    private static boolean isBlank(Object obj) {
        if (obj == null) return true;

        if (obj instanceof String) {
            return obj.toString().replace(" ", "").trim().equals("");
        }
        return false;
    }

    public boolean isFirst() {
        return isFirst;
    }

    public void setFirst(boolean first) {
        isFirst = first;
    }

    public boolean isLast() {
        return isLast;
    }

    public void setLast(boolean last) {
        isLast = last;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getCurrent() {
        return current;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
